package meminin.id3;

import meminin.model.Label;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ID3Node {

    private Label classLabel;
    private String classLabelValue;
    private HashMap<String, ID3Node> children;

    @Override
    public String toString() {
        String temp = "";
        if (isBranch()) {
            temp += "{ Branch with label : " + getClassLabel().getDesc();
            Iterator it = children.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry) it.next();
                ID3Node node = (ID3Node) pairs.getValue();
                temp += ", value : "+pairs.getKey()+node.toString();
            }
            temp += "}";
        } else if (isLeaf()) {
            temp += "{ Leaf with value : " + getClassLabelValue() + "}";
        }
        return temp;
    }

    public void setChildren(HashMap<String, ID3Node> children) {
        this.children = children;
    }

    public void setClassLabelValue(String classLabelValue) {
        this.classLabelValue = classLabelValue;
    }

    public HashMap<String, ID3Node> getChildren() {
        return children;
    }

    public String getClassLabelValue() {
        return classLabelValue;
    }

    public void setClassLabel(Label classLabel) {
        this.classLabel = classLabel;
    }

    public Label getClassLabel() {
        return classLabel;
    }

    public ID3Node() {
        classLabelValue = "";
        classLabel = null;
        children = new HashMap<String, ID3Node>();
    }

    public void addChild(String labelValue, ID3Node child) {
        children.put(labelValue, child);
    }

    public void removeChildren(String labelValue) {
        children.remove(labelValue);
    }

    public ID3Node getChildFromLabel(Label label) {
        Iterator it = children.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            ID3Node node = (ID3Node) pairs.getValue();
            if (node.getClassLabel().getDesc().equals(label.getDesc())) {
                return node;
            }
        }
        return null;
    }

    public ID3Node getChildFromValue(String labelValue) {
        return (ID3Node) children.get(labelValue);
    }

    public Boolean isBranch() {
        return (!isLeaf());
    }

    public Boolean isLeaf() {
        return children.isEmpty();
    }
}
